<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="1000"
    destroy-on-close
    :disabled="showonly"
    draggable
    overflow
    @closed="$emit('closed')"
  >
    <sk-tabs type="border-card">
      <el-tab-pane ref="tab" label="基础信息">
        <el-form
          :model="form"
          :rules="rules"
          ref="dialogForm"
          label-width="100px"
          label-position="right"
        >
          <el-row justify="space-evenly">
            <el-col :span="24">
              <el-descriptions title="收货信息" :column="4" border>
                <el-descriptions-item
                  label="采购形态"
                  min-width="100"
                  label-class-name="light-blue"
                >
                  <skDicSelect
                    v-model="form.type_id"
                    :code="'type'"
                    :placeholder="'请选择形态'"
                    :editsource="'default'"
                  >
                  </skDicSelect>
                </el-descriptions-item>
                <el-descriptions-item
                  label="批号"
                  min-width="100"
                  label-class-name="light-blue"
                >
                  <skAutoChoose
                    v-model="form.lot_name"
                    :name="'lot'"
                    :params="[
                      { material_id: form.material_id },
                      { company_id: form.company_id },
                    ]"
                  >
                  </skAutoChoose>
                </el-descriptions-item>
                <el-descriptions-item label="颜色" min-width="100">
                  <skAutoChoose
                    v-model="form.color.name"
                    :name="'color'"
                    :assign="form"
                    :browserShow="false"
                  >
                  </skAutoChoose>
                </el-descriptions-item>
                <el-descriptions-item label="缸号" min-width="100">
                  <el-input v-model="form.dyelot"></el-input>
                </el-descriptions-item>
                <el-descriptions-item
                  label="实际"
                  min-width="100"
                  label-class-name="light-blue"
                >
                  <el-input v-model="form.fect_quantity" @keyup="setAmount">
                    <template #prepend>
                      <el-input
                        v-model="form.fect_auxiliary_quantity"
                        @keyup="setMaterialQuantity"
                        placeholder="包数"
                      />
                    </template>
                  </el-input>
                </el-descriptions-item>
                <el-descriptions-item
                  label="码单"
                  min-width="100"
                  label-class-name="light-blue"
                >
                  <el-input v-model="form.quantity" @keyup="setAmount">
                    <template #prepend>
                      <el-input
                        v-model="form.auxiliary_quantity"
                        @keyup="setQuantity"
                        placeholder="包数"
                      />
                    </template>
                  </el-input>
                </el-descriptions-item>
                <el-descriptions-item
                  label="单价"
                  min-width="100"
                  label-class-name="light-blue"
                >
                  <el-input v-model="form.price" @keyup="setAmount"></el-input>
                </el-descriptions-item>
                <el-descriptions-item
                  label="金额"
                  min-width="100"
                  label-class-name="light-blue"
                >
                  <el-input v-model="form.amount" @keyup="setPrice"></el-input>
                </el-descriptions-item>
                <!-- PurchaseTarget -->
                <el-descriptions-item
                  label="采购去向"
                  min-width="100"
                  label-class-name="light-blue"
                >
                  <skDicSelect
                    v-model="form.target_id"
                    :code="'PurchaseTarget'"
                    :placeholder="'请选择'"
                  >
                  </skDicSelect>
                </el-descriptions-item>

                <el-descriptions-item
                  label="库位"
                  min-width="100"
                  label-class-name="light-blue"
                  v-if="form.target_id == 1"
                >
                  <skDicSelect
                    :code="'defaultLocation'"
                    v-model="form.location_id"
                    :editsource="'default'"
                  >
                  </skDicSelect>
                </el-descriptions-item>
                <el-descriptions-item min-width="100" v-if="form.target_id == 1">
                </el-descriptions-item>
                <el-descriptions-item min-width="100" v-if="form.target_id == 1">
                </el-descriptions-item>

                <el-descriptions-item
                  label="加工厂"
                  min-width="100"
                  label-class-name="light-blue"
                  v-if="form.target_id == 2"
                >
                  <skAutoChoose
                    v-model="form.outfactory.name"
                    :name="'company'"
                    :asKey="'outfactory_id'"
                    :assign="form"
                    :browserShow="false"
                    :params="[{ finance_id: -1 }]"
                  >
                  </skAutoChoose>
                </el-descriptions-item>

                <el-descriptions-item
                  label="工艺"
                  min-width="100"
                  label-class-name="light-blue selfcss"
                  v-if="form.target_id == 2"
                >
                  <skAutoChoose
                    v-model="form.bom.name"
                    :name="'bom'"
                    :assign="form"
                    :browserShow="false"
                    @select="bomSelect"
                  >
                    <template #prepend>
                      <el-input
                        v-model="form.outsourcing_price"
                        placeholder="单价"
                        style="width: 80px"
                      />
                    </template>
                  </skAutoChoose>
                </el-descriptions-item>
                <el-descriptions-item
                  label="收货形态"
                  min-width="100"
                  label-class-name="light-blue"
                  v-if="form.target_id == 2"
                >
                  <skDicSelect
                    :code="'type'"
                    v-model="form.outtype_id"
                    :editsource="'default'"
                    label-class-name="light-blue"
                  >
                  </skDicSelect>
                </el-descriptions-item>
                <el-descriptions-item label="单据日期" min-width="100">
                  <el-date-picker
                    v-model="main.billdate"
                    type="date"
                    value-format="YYYY-MM-DD"
                    placeholder="请选择日期"
                  />
                </el-descriptions-item>
                <el-descriptions-item label="运输单" min-width="100">
                  <skAutoChoose
                    v-model="form.car.code"
                    :name="'car'"
                    :assign="form"
                    @select="selectCar"
                  >
                  </skAutoChoose>
                </el-descriptions-item>
                <el-descriptions-item label="运输商" min-width="100">
                  <skAutoChoose
                    v-model="form.car.company.name"
                    :name="'factory'"
                    :asKey="'company_id'"
                    :assign="form.car"
                    :params="[{ category_id: 54 }]"
                  >
                  </skAutoChoose>
                </el-descriptions-item>
                <el-descriptions-item label="运费" min-width="100">
                  <el-input v-model="form.car.amount" clearable></el-input>
                </el-descriptions-item>

                <el-descriptions-item label="备注" min-width="100">
                  <el-input v-model="form.remark" clearable></el-input>
                </el-descriptions-item>
                <el-descriptions-item>
                  <template #label>是否含税</template>
                  <el-switch
                    v-model="form.tax_id"
                    :active-value="1"
                    :inactive-value="0"
                  ></el-switch>
                </el-descriptions-item>
              </el-descriptions>
            </el-col>
          </el-row>
          <el-row justify="space-evenly">
            <el-col :span="24">
              <el-descriptions title="订单信息" :column="3" border label-align="right">
                <el-descriptions-item>
                  <template #label>供应商</template>
                  {{ form.company.name }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template #label>供应商余额</template>
                  {{ form.company.finance_balance + form.company.balance }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template #label>订单日期</template>
                  {{ form.billdate }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template #label>交期</template>
                  {{ form.arrival_date }}
                </el-descriptions-item>

                <el-descriptions-item>
                  <template #label>产品</template>
                  {{ form.material.spec }}
                  {{ form.material.name }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template #label>形态</template>
                  {{ form.type.name }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template #label>批号</template>
                  {{ form.lot.name }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template #label>颜色</template>
                  {{ form.color.name }}
                </el-descriptions-item>

                <el-descriptions-item>
                  <template #label>计划数量</template>
                  {{ form.quantity }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template #label>计划单价</template>
                  {{ form.price }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template #label>金额</template>
                  {{ form.amount }}
                </el-descriptions-item>

                <el-descriptions-item>
                  <template #label>剩余未交</template>
                  {{ form.remain_quantity }}
                </el-descriptions-item>

                <el-descriptions-item>
                  <template #label>备注</template>
                  {{ form.remark }}
                </el-descriptions-item>
              </el-descriptions>
            </el-col>
          </el-row>
        </el-form>
      </el-tab-pane>
      <el-tab-pane ref="tab1" label="附件">
        <sk-upload v-model="form.attachments" :limit="10" draggable> </sk-upload>
      </el-tab-pane>
    </sk-tabs>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()" v-if="!showonly"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
import skDicSelect from "@/components/skDicSelect";
import skAutoChoose from "@/components/skAutoChoose";
import skAutoComplete from "@/components/skAutoComplete";
export default {
  emits: ["success", "closed"],
  components: { skDicSelect, skAutoChoose, skAutoComplete },
  data() {
    return {
      mode: "edit",

      titleMap: {
        add: "新增",
        edit: "编辑",
      },
      main: {},
      form: {
        id: "",
        company: {
          name: "",
        },
        outtype_id: null,
        color: {},
        company_id: "",
        outfactory: {},
        tax_id: 0,
        outfactory_id: 0,
        car: { company: {} },
        location_id: 1146,
        location: { name: "染厂暂存仓库" },
        bom: {},
        target_id: 1, //采购入库/直发
        attachments: [],
        billdate: this.$TOOL.SYS_DATE(),
      },
      rules: {
        company_id: [{ required: true, message: "请选择供应商" }],
      },
      visible: false,
      isSaveing: false,
    };
  },
  computed: {
    showonly() {
      return (this.main.id ?? 0) > 0;
    },
  },
  mounted() {},
  methods: {
    computeAmount(row) {
      row["tax_price"] = (row.price * (1 + row.tax / 100)).toFixed(2);
      row["amount"] = (row.quantity * row.price).toFixed(2);
      row["tax_amount"] = (row.quantity * row.price * (1 + row.tax / 100)).toFixed(2);
    },
    bomSelect({ type_id }) {
      this.form.outtype_id = type_id;
    },

    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;

      return this;
    },
    selectCar(data) {
      if (typeof data.code == "undefined") {
        data = { company: {} };
      }
      this.form.car = data;
    },
    setMaterialQuantity() {
      this.form.fect_quantity = this.form.fect_auxiliary_quantity * 25;
      this.form.auxiliary_quantity = this.form.fect_auxiliary_quantity;
      this.setQuantity();
    },
    setQuantity() {
      this.form.quantity = this.form.auxiliary_quantity * 25;
      this.setAmount();
    },
    setAmount() {
      this.form.amount = this.form.quantity * this.form.price;
    },
    setPrice() {
      this.form.price = this.form.amount / this.form.quantity;
    },

    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async valid => {
        if (valid) {
          this.isSaveing = true;
          delete this.form.lot_id;
          this.main.details = [this.form];
          var res = await this.$API.purchase.storage.save.post(this.main);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });
    },
    //表单注入数据
    setStorageData(data) {
      this.main = data.main;
      Object.assign(this.form, this.$TOOL.objCopy(this.$TOOL.removeNull(data)));
    },
    //表单注入数据
    setData(data) {
      Object.assign(this.form, this.$TOOL.objCopy(data));

      this.form.purchase_order_id = data.id;
      this.form.fect_quantity = this.form.quantity = this.form.remain_quantity.toFixed(2);
      this.form.fect_auxiliary_quantity = this.form.auxiliary_quantity = (
        this.form.remain_quantity / 25
      ).toFixed(2);
      this.main = {
        department_id: this.form.department_id,
        company_id: this.form.company_id,
        iscustomer: this.form.iscustomer,
        attachment: this.form.attachment,
        details: [],
      };
      delete this.form.id;
      delete this.form.code;
      delete this.form.material_quantity;
      delete this.form.material_auxiliary_quantity;
      delete this.form.arrival_date;
      this.setAmount();
    },
  },
};
</script>

<style></style>
